package com.okonna.selenium.tide.managers;

import java.io.File;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.server.RemoteControlConfiguration;

import com.okonna.selenium.tide.workers.*;

import com.thoughtworks.selenium.DefaultSelenium;

public class RCManager
{
    private static RCManager instance = null;
    private String strServerHost, strBrowser, strURL;
    private int intPort;
    
    private File serverLogFIle;
    
    private RCServerWorker selRCServerWorker;
    private RCClientWorker selRCClientWorker;

    private RCManager() // MUST remain private, prevents instantiation.
    {
		System.out.println(this.getClass().getSimpleName() + ".RCManager()");
    }

    public static RCManager getInstance()
    {
		System.out.println("RCManager.getInstance()");
		if (instance == null)
		{
		    instance = new RCManager();
		}
		return instance;
    }

    public void startClientSession(Configuration config) throws Exception
    {
		System.out.println(this.getClass().getSimpleName() + ".startClientSession()");
		if (!config.getKeys().hasNext())
		{
			System.out.println("Cannot run client without application.properties.  Please set your configuration and restart your session.");
			return;
		}
		strServerHost = config.getString("Server.Host");
		intPort = config.getInt("Server.Port");
		strBrowser = config.getString("Browser.StartCommand");
		strURL = config.getString("Browser.URL");
		selRCClientWorker = new RCClientWorker(new DefaultSelenium(strServerHost, intPort, strBrowser, strURL));
		selRCClientWorker.startClient();
		return;
    }

    public void stopClientSession() throws Exception
    {
		System.out.println(this.getClass().getSimpleName() + ".stopClientSession()");
		selRCClientWorker.stopClient();
		selRCClientWorker = null;
		return;
    }
    
    public void restartClientSession() throws Exception
    {
		System.out.println(this.getClass().getSimpleName() + ".restartClientSession()");
		selRCClientWorker.stopClient();
		selRCClientWorker.startClient();
		return;
    }
    
	public void startServerSession(Configuration config) throws Exception
	{
		System.out.println(this.getClass().getSimpleName() + ".startServerSession()");
		if (!config.getKeys().hasNext())
		{
			System.out.println("Cannot run server without application.properties.  Please set your configuration and restart your session.");
			return;
		}
		RemoteControlConfiguration selRCServerConfig = new RemoteControlConfiguration();
		intPort = config.getInt("Server.Port");
		selRCServerConfig.setPort(intPort);
	    try
	    {
	    	serverLogFIle = FileUtils.getFile("selenium-server.log");
		} 		
		catch (Exception e)
		{
			   System.out.println("\nException:\n" + e + "\n");
		}
		selRCServerConfig.setLogOutFile(serverLogFIle);
		selRCServerWorker = new RCServerWorker(selRCServerConfig);
		selRCServerWorker.startServer();
		return;
	}

	public void stopServerSession() throws Exception
	{
		System.out.println(this.getClass().getSimpleName() + ".stopServerSession()");
		selRCServerWorker.stopServer();
		selRCServerWorker = null;
		return;
	}
	
    public void openPage(String strURLPath) throws Exception
    {
		System.out.println(this.getClass().getSimpleName() + ".openPage(" + strURLPath + ")");
		selRCClientWorker.openPage(strURLPath);
		return;
    }

    public String dumpText(String strLocator) throws Exception
    {
		System.out.println(this.getClass().getSimpleName() + ".dumpText(" + strLocator + ")");
		return selRCClientWorker.dumpText(strLocator);
    }
    public void doCommand(String strCommand, String strTarget, String strValue) throws Exception
	{
    	return;
    }
}
